package com.md.notes.model.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * 数据库帮助类
 * 负责数据库的创建、升级等操作
 */
public class DatabaseHelper extends SQLiteOpenHelper {
    // 数据库名称
    private static final String DATABASE_NAME = "notes.db";
    // 数据库版本
    private static final int DATABASE_VERSION = 1;
    // 表名
    public static final String TABLE_NOTE = "note";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // 创建笔记表
        String CREATE_NOTE_TABLE = "CREATE TABLE " + TABLE_NOTE + "("
                + "id INTEGER PRIMARY KEY AUTOINCREMENT,"
                + "title TEXT,"
                + "createDate INTEGER," //存储更新时间戳
                + "updateDate INTEGER,"  // 新增updateDate字段，存储更新时间戳
                + "specNo TEXT,"
                + "content TEXT,"
                + "appNo TEXT"
                + ")";
        db.execSQL(CREATE_NOTE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // 如果旧表存在，删除旧表
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NOTE);
        // 创建新表
        onCreate(db);
    }
} 